package com.paireach.bw.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.FrameLayout;

import com.paireach.bw.R;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.bean.OrderResult;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.fragment.AllFrag245;
import com.paireach.bw.fragment.CommonFrag212;
import com.paireach.bw.fragment.CommonFrag220;
import com.paireach.bw.fragment.CommonFrag226;
import com.paireach.bw.fragment.CommonFrag228;
import com.paireach.bw.fragment.CommonFrag230;
import com.paireach.bw.fragment.CommonFrag238;
import com.paireach.bw.fragment.CommonFrag240;
import com.paireach.bw.fragment.ErrorFragment;
import com.paireach.bw.fragment.KaFrag220;
import com.paireach.bw.fragment.KaFrag226;
import com.paireach.bw.fragment.KaFrag228;
import com.paireach.bw.fragment.KaFrag230;
import com.paireach.bw.fragment.KaFrag238;
import com.paireach.bw.fragment.KaFrag240;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.GsonUtil;
import com.paireach.bw.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${cqc} on 2017/6/19.
 */

public class CommonActivity extends BaseActivity {

    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    private String orderCode;//负载单号
    private FragmentManager fm;
    private boolean fromNotification;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        ButterKnife.bind(this);
        orderCode = getIntent().getStringExtra("orderCode");
        fromNotification = getIntent().getBooleanExtra("fromNotification", false);
        fm = getSupportFragmentManager();
        getOrderCodeDetail();
    }


    public void getOrderCodeDetail() {
        showProgressDialog();
        new WayBillRequest().getOrderCodeDetail(phoneNumber, orderCode);
    }


    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        showToast(event.getErrorMessage());
        if (event.getRequestTag() == RequestTag.GET_ORDER_CODE_DETAIL || event.getRequestTag() == RequestTag.GET_ORDER_STATUS) {
            finish();
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.GET_ORDER_CODE_DETAIL) {
            if (TextUtils.isEmpty(event.getJson())) {
                showToast("没有获取到运单详情");
            } else {
                OrderResult orderResult = GsonUtil.jsonToBean(event.getJson(), OrderResult.class);
                if (orderResult.getStatus() == 1) {
                    FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//                    String status = orderResult.getOrders().get(0).getSHPM_STATUS();

                    List<Integer> statusList = new ArrayList<>();
                    for (int i = 0; i < orderResult.getOrders().size(); i++) {
                        String status = orderResult.getOrders().get(i).getSHPM_STATUS();
                        statusList.add(Integer.parseInt(status));
                    }
                    int statusInteger = Collections.min(statusList);
                    String status = String.valueOf(statusInteger);
                    switch (status) {
                        case "212":
                            transaction.replace(R.id.frameLayout, CommonFrag212.getInstance(orderResult,fromNotification)).commitAllowingStateLoss();
                            break;
                        case "220":
                            transaction.replace(R.id.frameLayout, CommonFrag220.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "226":
                            transaction.replace(R.id.frameLayout, CommonFrag226.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "228":
                            transaction.replace(R.id.frameLayout, CommonFrag228.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "230":
                            transaction.replace(R.id.frameLayout, CommonFrag230.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "238":
                            transaction.replace(R.id.frameLayout, CommonFrag238.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "240":
                            transaction.replace(R.id.frameLayout, CommonFrag240.getInstance(orderResult)).commitAllowingStateLoss();
                            break;
                        case "241"://收货入厂拒绝
                            to240Or245(orderResult, transaction);
                            break;
                        case "245":
                            to240Or245(orderResult, transaction);
                            break;
                    }
                }
            }
        } else if (event.getRequestTag() == RequestTag.GET_ORDER_STATUS) {
            if (TextUtils.isEmpty(event.getJson())) {
                showToast("获取数据失败");
            } else {
                LogUtil.d(event.getJson());
            }
        }
    }

    private void to240Or245(OrderResult orderResult, FragmentTransaction transaction) {
        int number = 0;
        for (int i = 0; i < orderResult.getOrders().size(); i++) {
            String shpm_status = orderResult.getOrders().get(i).getSHPM_STATUS();
            if ("241".equals(shpm_status) || "245".equals(shpm_status)) {
                number = number + 1;
            }
        }
        if (number == orderResult.getOrders().size()) {
            transaction.replace(R.id.frameLayout, AllFrag245.getInstance(orderCode)).commitAllowingStateLoss();
        } else {
            transaction.replace(R.id.frameLayout, CommonFrag240.getInstance(orderResult)).commitAllowingStateLoss();
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        switch (event.getTag()) {
            case "GetOrderCodeDetail":
                getOrderCodeDetail();
                break;
            case "异常签收":
                getOrderCodeDetail();
                break;
            case "加载失败":
                FragmentTransaction transaction = fm.beginTransaction().setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                transaction.replace(R.id.frameLayout, ErrorFragment.getInstance(orderCode)).commitAllowingStateLoss();
                break;
        }
    }
}
